<?php

/**
 * This is the model class for table "{{words}}".
 *
 * The followings are the available columns in table '{{words}}':
 * @property integer $id
 * @property string $src
 * @property string $source
 
 * @property string $title
 * @property string $title_c 
 * @property string $title_ex 
 
 * @property string $target
 * @property string $type
 * @property string $company
 
 * @property string $active_w
 * @property string $d_start_w
 * @property string $d_end_w
 
 * @property integer $show_geot
 * @property string $country
 * @property string $city
 * @property string $region 
 * @property integer $geot_invert 
 
 * @property string $source_ban
 * @property string $width_ban
 * @property string $height_ban
 * @property string $theme_ban
 * @property integer $need_to_close_ban
 
 * @property integer $show_header_ban
 * @property string $caption_ban
 * @property string $ccap_ban
  
 * @property integer $show_footer_ban
 * @property string $footer_ban
 * @property string $cfoot_ban
 
 * @property string $source_vid
 * @property string $width_vid
 * @property string $height_vid
 * @property string $screen_vid
 * @property string $screen_vid_f
 * @property integer $ap_vid
 * @property string $theme_vid
 * @property integer $need_to_close_vid
 
 * @property integer $show_header_vid
 * @property string $caption_vid
 * @property string $ccap_vid 
 
 * @property integer $show_footer_vid
 * @property string $footer_vid
 * @property string $cfoot_vid 
 */
class Words extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Words the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{words}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('title_c, show_geot, geot_invert, need_to_close_ban, show_header_ban, show_footer_ban, ap_vid, need_to_close_vid, show_header_vid, show_footer_vid', 'numerical', 'integerOnly'=>true),
			array('src, source, title, title_ex, type, company, active_w, d_start_w, d_end_w, country, city, region, width_ban, height_ban, theme_ban, width_vid, height_vid, theme_vid, ccap_ban, cfoot_ban, ccap_vid, cfoot_vid', 'length', 'max'=>100),
			array('target, source_ban, source_vid, screen_vid', 'length', 'max'=>300),
			array('footer_ban, caption_ban, caption_vid, footer_vid', 'length', 'max'=>1000),
			array('screen_vid_f', 'safe'),
			array('screen_vid_f', 'file', 'types'=>'jpg, gif, png', 'allowEmpty' => true, 'maxSize' => 1024 * 1024 * 5, 'safe'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, srcID, title, title_ex, target, type, company, active_w, d_start_w, d_end_w, title_c, show_geot, country, city, region, geot_invert, source_ban, width_ban, height_ban, theme_ban, need_to_close_ban, show_header_ban, caption_ban, show_footer_ban, footer_ban, source_vid, width_vid, height_vid, screen_vid, ap_vid, theme_vid, need_to_close_vid, show_header_vid, caption_vid, show_footer_vid, footer_vid, screen_vid_f, ccap_ban, cfoot_ban, ccap_vid, cfoot_vid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'src' => 'Материал',
			'source' => 'Источник',
			'title' => 'Слово',
			'title_с' => 'Уточнить',
			'title_ex' => 'Слово',
			'target' => 'Назначение',
			'type' => 'Тип',
			'company' => 'Компания',
			'active_w' => 'Состояние',
			'd_start_w' => 'Дата начала',
			'd_end_w' => 'Дата окончания',
			'show_geot' => 'Геотаргетинг',
			'country' => 'Страна',
			'city' => 'Город',
			'region' => 'Регион',
			'geot_invert' => 'Все кроме выбранных',
			'source_ban' => 'Источник',
			'width_ban' => 'Ширина',
			'height_ban' => 'Высота',
			'theme_ban' => 'Фон',
			'need_to_close_ban' => 'Требует закрытия',
			'show_header_ban' => 'Показать заголовок',
			'caption_ban' => 'Заголовок',
			'ccap_ban' => 'Фон заголовка',
			'show_footer_ban' => 'Показать footer',
			'footer_ban' => 'Footer',
			'cfoot_ban' => 'Фон footer',
			'source_vid' => 'Источник',
			'width_vid' => 'Ширина',
			'height_vid' => 'Высота',
			'screen_vid' => 'Заставка',
			'screen_vid_f' => 'Файл заставки',
			'ap_vid' => 'Автозапуск',
			'theme_vid' => 'Фон',
			'need_to_close_vid' => 'Требует закрытия',
			'show_header_vid' => 'Показать заголовок',
			'caption_vid' => 'Заголовок',
			'ccap_vid' => 'Фон заголовка',
			'show_footer_vid' => 'Показать footer',
			'footer_vid' => 'Footer',
			'cfoot_vid' => 'Фон footer',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('src',$this->src);
		$criteria->compare('source',$this->source);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('title_c',$this->title_c,true);
		$criteria->compare('title_ex',$this->title_ex,true);
		$criteria->compare('target',$this->target,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('active_w',$this->active_w,true);
		$criteria->compare('d_start_w',$this->d_start_w,true);
		$criteria->compare('d_end_w',$this->d_end_w,true);
		$criteria->compare('show_geot',$this->show_geot,true);
		$criteria->compare('country',$this->country,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('region',$this->region,true);
		$criteria->compare('geot_invert',$this->geot_invert,true);
		$criteria->compare('source_ban',$this->source_ban,true);
		$criteria->compare('width_ban',$this->width_ban,true);
		$criteria->compare('height_ban',$this->height_ban,true);
		$criteria->compare('theme_ban',$this->theme_ban,true);
		$criteria->compare('need_to_close_ban',$this->need_to_close_ban);
		$criteria->compare('show_header_ban',$this->show_header_ban);
		$criteria->compare('caption_ban',$this->caption_ban,true);
		$criteria->compare('show_footer_ban',$this->show_footer_ban);
		$criteria->compare('footer_ban',$this->footer_ban,true);
		$criteria->compare('source_vid',$this->source_vid,true);
		$criteria->compare('width_vid',$this->width_vid,true);
		$criteria->compare('height_vid',$this->height_vid,true);
		$criteria->compare('screen_vid',$this->screen_vid,true);
		$criteria->compare('screen_vid_f',$this->screen_vid_f,true);
		$criteria->compare('ap_vid',$this->ap_vid);
		$criteria->compare('theme_vid',$this->theme_vid,true);
		$criteria->compare('need_to_close_vid',$this->need_to_close_vid);
		$criteria->compare('show_header_vid',$this->show_header_vid);
		$criteria->compare('caption_vid',$this->caption_vid,true);
		$criteria->compare('show_footer_vid',$this->show_footer_vid);
		$criteria->compare('footer_vid',$this->footer_vid,true);
		$criteria->compare('ccap_ban',$this->ccap_ban,true);
		$criteria->compare('cfoot_ban',$this->cfoot_ban,true);
		$criteria->compare('ccap_vid',$this->ccap_vid,true);
		$criteria->compare('cfoot_vid',$this->cfoot_vid,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	protected function beforeSave() 
	{
		if(parent::beforeSave()) {
			if ($this->d_start_w) { $this->d_start_w = date('Y-m-d', strtotime($this->d_start_w)); }
			if ($this->d_end_w) { $this->d_end_w = date('Y-m-d', strtotime($this->d_end_w)); }
			return true;
		} else { return false; }
	}

	protected function afterFind() 
	{
		if ($this->d_start_w) { 
			$date_d_start_w = date('d.m.Y', strtotime($this->d_start_w));
			$this->d_start_w = $date_d_start_w;
		}
		if ($this->d_end_w) { 
			$date_d_end_w = date('d.m.Y', strtotime($this->d_end_w));
			$this->d_end_w = $date_d_end_w;
		}
		parent::afterFind();
	}

	protected function beforeDelete() 
	{
		if (parent::beforeDelete()) {
		
			if ($this->source) { @unlink(Yii::getPathOfAlias('webroot.images.uploads').'/'.$this->source .'.html'); }
			
			if ($this->source_ban) {
				$doc_ban = new DOMDocument();
				$doc_ban->loadHTML($this->source_ban);
				$imageTags = $doc_ban->getElementsByTagName('img');
				foreach($imageTags as $tag) {
					@unlink(Yii::getPathOfAlias('webroot').'/'.$tag->getAttribute('src'));
				}			
			}
			
			if ($this->source_vid) {			
				preg_match('/href="([^\s"]+)/', $this->source_vid, $match);
				if ($match) {
					//$file = explode("images/files/", $match[1]);
					@unlink(Yii::getPathOfAlias('webroot').'/'.$match[1]);
					//$del = Yii::getPathOfAlias('webroot').'/'.$match[1];
					//$file = Yii::getPathOfAlias('webroot.images.files').'/log.txt';
					//file_put_contents($file, $del); 
				}
			}
			
			preg_match('/src="([^"]*)"/i', $this->screen_vid, $array);
			if ($array) { @unlink(Yii::getPathOfAlias('webroot.images.files').'/'.$array[1]); }
			
			//$file = Yii::getPathOfAlias('webroot.images.files').'/log.txt';
			//file_put_contents($file, $del); 
			return true;
			
		} else { return false; }
	} 	
}